<?php

namespace App\Admin\Controllers;

use App\Models\Misdeed;
use App\Models\ProjectClosureUser;
use App\Models\Engineering;
use App\Models\Company;
use App\Models\Team;
use Carbon\Carbon;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Admin;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;
use App\Admin\Forms\ClosureImportForm;
use App\Admin\Actions\DownloadTemplate;
use Dcat\Admin\Widgets\Modal;
use Dcat\Admin\Widgets\Card;
use App\Admin\Renderable\ClosureUserDetails;
use App\Admin\Extensions\ExcelExpoter;
use App\Models\TypeWork;


class ClosureWorkUserController extends AdminController
{

    protected $title = '历史结项务工人员';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ProjectClosureUser(), function (Grid $grid) {
            $grid->disableViewButton();
            $grid->export()->disableExportAll();
            $grid->perPages([10, 20, 30, 40, 50,100,200,300,400,500]);

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->like('engineering', '所属工程')->select(function ($status){
                    $Adminid  = Admin::user()->id;
                    if($Adminid != 1){
                        $engineering_name = DB::table('engineering')->where('manager','like','%'.$Adminid.'%')->where('deleted_at',null)->pluck('engineering_name');
                    }else{
                        $engineering_name = DB::table('engineering')->where('deleted_at',null)->pluck('engineering_name');
                    }
                    $arr = [];
                    foreach ($engineering_name as $key => $value){
                        $arr["$value"] = $value;
                    }
                    return $arr;
                })->width(5);
                $filter->like('id_card', '身份证号')->width(5);
                $filter->like('name', '姓名')->width(5);
                $filter->equal('photo','手机号')->width(5);


                $filter->like('team', '所属队伍')->select(function ($team){
                    $name = DB::table('team')->where('deleted_at',null)->pluck('name');
                    $arr = [];
                    foreach ($name as $key => $value){
                        $arr["$value"] = $value;
                    }
                    return $arr;
                })->width(5);

                $filter->equal('type_work','工种')->select(function ($team){
                    $name = DB::table('type_work')->pluck('name');
                    $arr = [];
                    foreach ($name as $key => $value){
                        $arr["$value"] = $value;
                    }
                    return $arr;
                })->width(5);

            });

            if (Admin::user()->can('inspector')) {
                $grid->tools(function (Grid\Tools $tools) {
                    $tools->append(Modal::make()
                        // 大号弹窗
                        ->lg()
                        // 弹窗标题
                        ->title('上传文件')
                        // 按钮
                        ->button('<button class="btn btn-primary"><i class="feather icon-upload"></i> 导入结项项目人员数据</button>')
                        // 弹窗内容
                        ->body(ClosureImportForm::make()));
                    // 下载导入模板
                    $tools->append(DownloadTemplate::make()->setKey('test_question'));
                });
            }



            if(Admin::user()->sensitive == 0){
                $grid->export(new ExcelExpoter());
            }


            $Adminid  = Admin::user()->id;
            if(Admin::user()->whole == 1){
                $engineering_names = DB::table('engineering')->where('manager','like','%'.$Adminid.'%')->pluck('engineering_name');
                $grid->model()->whereIn('engineering', $engineering_names);
            }
            if(Admin::user()->whole == 2){
                $grid->model()->where('uploader', $Adminid);
            }



            $grid->model()->where('misdeed', 1);

            $grid->number('序号');
            $grid->column('name');

            $grid->column('company','所属公司');
            $grid->column('team','所属队伍');
            $grid->column('engineering','所属工程')->limit(10);


            $grid->column('type_work','工种');
            $grid->column('age','年龄');
            $grid->column('uploader','数据来源')->display(function ($uploader) {
                return DB::table('admin_users')->where('id',$uploader)->value('username');
            });


              if(Admin::user()->sensitive == 0){
                  $grid->column('人员详情')->display('人员详情')->modal(function (Grid\Displayers\Modal $modal) {
                      $modal->title('人员详情');
                      $modal->icon('feather icon-edit');
                      return ClosureUserDetails::make()->payload(['id_card' => $this->id_card,'engineering'=>$this->engineering]);
                  });
              }




//            $grid->column('notes');


//            $grid->column('nationality');
//            $grid->column('registered_residence');
//            $grid->column('educational');
//            $grid->column('employment_method');
//            $grid->column('marital');
//            $grid->column('emergency_contact');
//            $grid->column('emergency_contact_mobile');
//            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WorkUser(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('photo');
            $show->field('id_card');
            $show->field('sex');
            $show->field('nationality');
            $show->field('engineering');
            $show->field('registered_residence');
            $show->field('mobile');
            $show->field('educational');
            $show->field('employment_method');
            $show->field('marital');
            $show->field('emergency_contact');
            $show->field('emergency_contact_mobile');
            $show->field('status');
            $show->field('misdeed');
            $show->field('notes');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ProjectClosureUser(), function (Form $form) {

            $form->tab('基本信息', function (Form $form) {
                $form->select('company','所属公司')->options(function ($id) {
                    return Company::where('name','<>','')->pluck('name', 'name');
                });
                $form->select('team','所属队伍')->options(function ($id) {
                    return Team::where('name','<>','')->pluck('name', 'name');
                });
                $form->select('engineering','所属工程')->options(function ($id) {
                    return Engineering::where('engineering_name','<>','')->pluck('engineering_name', 'engineering_name');
                });
                $form->text('name','姓名');
                $form->mobile('mobile','手机号')->type("number")->required();
                $form->select('type_work','工种')->options(function ($id) {
                    return TypeWork::pluck('name', 'name');
                });
                $form->datetime('enter_time','入职时间')->format('YYYY-MM-DD')->required();
                $form->datetime('exit_time','离职时间')->format('YYYY-MM-DD');
                $form->hidden('enter_date');
                $form->text('notes','备注');
            })->tab('身份证', function (Form $form) {
                $form->image('photo_portrait','身份证人像面');
                $form->image('photo_emblem','身份证国徽面');
                $form->text('id_card','身份证号');
                $form->text('registered_residence','户口所在地');
                $form->text('bank_deposit','开户银行');
                $form->text('bank_number','银行卡号');
            })->tab('人员状态', function (Form $form) {
                $form->radio('status','是否离职')->options(['1' => '正常', '2'=> '离职'])->default('1');

                if (Admin::user()->can('lingdao')) {
                    $form->select('misdeed','不端行为')->options('misdeed_list');
                }

                $form->hidden('uploader');
            });


        });
    }

}
